package net.ddns.xcyyds.kungfu.procedures;

import net.minecraftforge.registries.ForgeRegistries;

import net.minecraft.world.phys.Vec3;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.Entity;
import net.minecraft.tags.TagKey;
import net.minecraft.sounds.SoundSource;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.core.Registry;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos;

import net.ddns.xcyyds.kungfu.network.KungFuModVariables;

public class Jump1Procedure {
	public static void execute(LevelAccessor world, double x, double y, double z, Entity entity) {
		if (entity == null)
			return;
		double x3 = 0;
		double z3 = 0;
		double x1 = 0;
		double z1 = 0;
		if (entity.getPersistentData().getDouble("jumpCount") >= 1 && !entity.isOnGround()) {
			if ((entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
					.orElse(new KungFuModVariables.PlayerVariables())).neiLi >= 0.8
					&& (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
							.orElse(new KungFuModVariables.PlayerVariables())).qingGong == 1) {
				x1 = entity.getDeltaMovement().x() * 1.1 + entity.getLookAngle().x * 0.4;
				z1 = entity.getDeltaMovement().z() * 1.1 + entity.getLookAngle().z * 0.4;
				entity.getPersistentData().putDouble("jumpCount", (entity.getPersistentData().getDouble("jumpCount") - 1));
				{
					double _setval = (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
							.orElse(new KungFuModVariables.PlayerVariables())).neiLi - 0.8;
					entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
						capability.neiLi = _setval;
						capability.syncPlayerVariables(entity);
					});
				}
				entity.setDeltaMovement(new Vec3(x1, 0.6, z1));
				if (world instanceof Level _level) {
					if (!_level.isClientSide()) {
						_level.playSound(null, new BlockPos(x, y, z), ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("ui.toast.in")),
								SoundSource.PLAYERS, (float) 0.6, (float) 0.6);
					} else {
						_level.playLocalSound(x, y, z, ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("ui.toast.in")),
								SoundSource.PLAYERS, (float) 0.6, (float) 0.6, false);
					}
				}
				if (world instanceof ServerLevel _level)
					_level.sendParticles(ParticleTypes.CLOUD, x, y, z, 14, 0.3, 0.5, 0.3, 0);
			}
			if ((entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
					.orElse(new KungFuModVariables.PlayerVariables())).neiLi >= 0.4
					&& (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
							.orElse(new KungFuModVariables.PlayerVariables())).qingGong == 2
					&& ((world.getBlockState(new BlockPos(x, y - 1, z))).getBlock() == Blocks.WATER
							|| (world.getBlockState(new BlockPos(x, y - 1, z))).getBlock() == Blocks.WATER
							|| (world.getBlockState(new BlockPos(x, y - 1, z))).getBlock() == Blocks.BUBBLE_COLUMN
							|| (world.getBlockState(new BlockPos(x, y - 1, z))).getBlock() == Blocks.SEAGRASS
							|| (world.getBlockState(new BlockPos(x, y - 1, z))).getBlock() == Blocks.TALL_SEAGRASS
							|| (world.getBlockState(new BlockPos(x, y - 1, z))).getBlock() == Blocks.KELP_PLANT
							|| (world.getBlockState(new BlockPos(x, y - 1, z))).getBlock() == Blocks.KELP
							|| (world.getBlockState(new BlockPos(x, y, z))).getBlock() == Blocks.WATER
							|| (world.getBlockState(new BlockPos(x, y, z))).getBlock() == Blocks.WATER
							|| (world.getBlockState(new BlockPos(x, y, z))).getBlock() == Blocks.BUBBLE_COLUMN
							|| (world.getBlockState(new BlockPos(x, y, z))).getBlock() == Blocks.KELP_PLANT
							|| (world.getBlockState(new BlockPos(x, y, z))).getBlock() == Blocks.TALL_SEAGRASS
							|| (world.getBlockState(new BlockPos(x, y, z))).getBlock() == Blocks.SEAGRASS
							|| (world.getBlockState(new BlockPos(x, y, z))).getBlock() == Blocks.KELP)
					&& new Object() {
						public double getSubmergedHeight(Entity _entity) {
							for (TagKey<Fluid> _fldtag : Registry.FLUID.getTagNames().toList()) {
								if (_entity.level.getFluidState(entity.blockPosition()).is(_fldtag))
									return _entity.getFluidHeight(_fldtag);
							}
							return 0;
						}
					}.getSubmergedHeight(entity) < 0.7) {
				{
					double _setval = (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
							.orElse(new KungFuModVariables.PlayerVariables())).neiLi - 0.4;
					entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
						capability.neiLi = _setval;
						capability.syncPlayerVariables(entity);
					});
				}
				entity.getPersistentData().putDouble("jumpCount", (entity.getPersistentData().getDouble("jumpCount") - 1));
				entity.setDeltaMovement(new Vec3((entity.getDeltaMovement().x() * 1.5), 0.4, (entity.getDeltaMovement().z() * 1.5)));
				if (world instanceof Level _level) {
					if (!_level.isClientSide()) {
						_level.playSound(null, new BlockPos(x, y, z),
								ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("ambient.underwater.exit")), SoundSource.PLAYERS,
								(float) 0.6, 1);
					} else {
						_level.playLocalSound(x, y, z, ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("ambient.underwater.exit")),
								SoundSource.PLAYERS, (float) 0.6, 1, false);
					}
				}
				if (world instanceof ServerLevel _level)
					_level.sendParticles(ParticleTypes.CLOUD, x, y, z, 14, 0.3, 0.5, 0.3, 0);
			}
			if ((entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
					.orElse(new KungFuModVariables.PlayerVariables())).neiLi >= 0.9
					&& (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
							.orElse(new KungFuModVariables.PlayerVariables())).qingGong == 3) {
				x3 = entity.getLookAngle().x * 0.4 + entity.getDeltaMovement().x() * 1.4;
				z3 = entity.getLookAngle().z * 0.4 + entity.getDeltaMovement().z() * 1.4;
				if (world.getBlockState(new BlockPos(x - 1, y, z)).isFaceSturdy(world, new BlockPos(x - 1, y, z), Direction.EAST)) {
					{
						double _setval = (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new KungFuModVariables.PlayerVariables())).neiLi - 0.9;
						entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
							capability.neiLi = _setval;
							capability.syncPlayerVariables(entity);
						});
					}
					entity.getPersistentData().putDouble("jumpCount", (entity.getPersistentData().getDouble("jumpCount") - 1));
					entity.setDeltaMovement(new Vec3((x3 + 0.2), 0.9, z3));
					if (world instanceof Level _level) {
						if (!_level.isClientSide()) {
							_level.playSound(null, new BlockPos(x, y, z), ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("ui.toast.in")),
									SoundSource.PLAYERS, (float) 0.8, (float) 0.8);
						} else {
							_level.playLocalSound(x, y, z, ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("ui.toast.in")),
									SoundSource.PLAYERS, (float) 0.8, (float) 0.8, false);
						}
					}
					if (world instanceof ServerLevel _level)
						_level.sendParticles(ParticleTypes.CLOUD, x, y, z, 14, 0.3, 0.5, 0.3, 0);
				} else if (world.getBlockState(new BlockPos(x + 1, y, z)).isFaceSturdy(world, new BlockPos(x + 1, y, z), Direction.WEST)) {
					{
						double _setval = (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new KungFuModVariables.PlayerVariables())).neiLi - 0.9;
						entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
							capability.neiLi = _setval;
							capability.syncPlayerVariables(entity);
						});
					}
					entity.getPersistentData().putDouble("jumpCount", (entity.getPersistentData().getDouble("jumpCount") - 1));
					entity.setDeltaMovement(new Vec3((x3 - 0.2), 0.9, z3));
					if (world instanceof Level _level) {
						if (!_level.isClientSide()) {
							_level.playSound(null, new BlockPos(x, y, z), ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("ui.toast.in")),
									SoundSource.PLAYERS, (float) 0.8, (float) 0.8);
						} else {
							_level.playLocalSound(x, y, z, ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("ui.toast.in")),
									SoundSource.PLAYERS, (float) 0.8, (float) 0.8, false);
						}
					}
					if (world instanceof ServerLevel _level)
						_level.sendParticles(ParticleTypes.CLOUD, x, y, z, 14, 0.3, 0.5, 0.3, 0);
				} else if (world.getBlockState(new BlockPos(x, y, z - 1)).isFaceSturdy(world, new BlockPos(x, y, z - 1), Direction.SOUTH)) {
					{
						double _setval = (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new KungFuModVariables.PlayerVariables())).neiLi - 0.9;
						entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
							capability.neiLi = _setval;
							capability.syncPlayerVariables(entity);
						});
					}
					entity.getPersistentData().putDouble("jumpCount", (entity.getPersistentData().getDouble("jumpCount") - 1));
					entity.setDeltaMovement(new Vec3(x3, 0.9, (z3 + 0.2)));
					if (world instanceof Level _level) {
						if (!_level.isClientSide()) {
							_level.playSound(null, new BlockPos(x, y, z), ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("ui.toast.in")),
									SoundSource.PLAYERS, (float) 0.8, (float) 0.8);
						} else {
							_level.playLocalSound(x, y, z, ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("ui.toast.in")),
									SoundSource.PLAYERS, (float) 0.8, (float) 0.8, false);
						}
					}
					if (world instanceof ServerLevel _level)
						_level.sendParticles(ParticleTypes.CLOUD, x, y, z, 14, 0.3, 0.5, 0.3, 0);
				} else if (world.getBlockState(new BlockPos(x, y, z + 1)).isFaceSturdy(world, new BlockPos(x, y, z + 1), Direction.NORTH)) {
					{
						double _setval = (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new KungFuModVariables.PlayerVariables())).neiLi - 0.9;
						entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
							capability.neiLi = _setval;
							capability.syncPlayerVariables(entity);
						});
					}
					entity.getPersistentData().putDouble("jumpCount", (entity.getPersistentData().getDouble("jumpCount") - 1));
					entity.setDeltaMovement(new Vec3(x3, 0.9, (z3 - 0.2)));
					if (world instanceof Level _level) {
						if (!_level.isClientSide()) {
							_level.playSound(null, new BlockPos(x, y, z), ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("ui.toast.in")),
									SoundSource.PLAYERS, (float) 0.8, (float) 0.8);
						} else {
							_level.playLocalSound(x, y, z, ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("ui.toast.in")),
									SoundSource.PLAYERS, (float) 0.8, (float) 0.8, false);
						}
					}
					if (world instanceof ServerLevel _level)
						_level.sendParticles(ParticleTypes.CLOUD, x, y, z, 14, 0.3, 0.5, 0.3, 0);
				} else {
					if (entity.getPersistentData().getDouble("jumpCount") >= 4
							&& (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
									.orElse(new KungFuModVariables.PlayerVariables())).neiLi >= 2) {
						{
							double _setval = (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
									.orElse(new KungFuModVariables.PlayerVariables())).neiLi - 2;
							entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
								capability.neiLi = _setval;
								capability.syncPlayerVariables(entity);
							});
						}
						entity.getPersistentData().putDouble("jumpCount", (entity.getPersistentData().getDouble("jumpCount") - 4));
						entity.setDeltaMovement(new Vec3((x3 * 1.2), 0.5, (z3 * 1.2)));
						if (world instanceof Level _level) {
							if (!_level.isClientSide()) {
								_level.playSound(null, new BlockPos(x, y, z),
										ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("ui.toast.in")), SoundSource.PLAYERS, (float) 0.8,
										(float) 0.8);
							} else {
								_level.playLocalSound(x, y, z, ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("ui.toast.in")),
										SoundSource.PLAYERS, (float) 0.8, (float) 0.8, false);
							}
						}
						if (world instanceof ServerLevel _level)
							_level.sendParticles(ParticleTypes.CLOUD, x, y, z, 14, 0.3, 0.5, 0.3, 0);
					}
				}
			}
		}
	}
}
